package com.hq.fxh.No4_XiaoXi.IM;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hq.fxh.R;
import com.hq.fxh.Utils.AppConstant;
import com.hq.fxh.Utils.Tool;

import java.util.List;

import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.UserInfo;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/10/9
 * @Email xlwenhao1858@sina.com
 */


public class Ladapte_imlist extends BaseAdapter {
    List<Conversation> data;

    public void setData(List<Conversation> data) {
        this.data = data;
    }

    @Override
    public int getCount() {
        return data == null ? 1 : data.size();
    }

    @Override
    public Conversation getItem(int i) {
        if (data == null) {
            return null;
        }
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 5;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder viewHolder = null;
        if (view == null || !(view.getTag() instanceof ViewHolder)) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_im_list, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        if (data == null) {
            viewHolder.layoutLiaoTian.setVisibility(View.GONE);
        } else {
            viewHolder.layoutLiaoTian.setVisibility(View.VISIBLE);

            UserInfo targetInfo = (UserInfo) data.get(position).getTargetInfo();
            Tool.setUrlToHeadImage(parent.getContext(), AppConstant.headimageUrl, viewHolder.imageIMItemHeadPic);
            String nickname = targetInfo.getNickname();
            if (nickname != null) {
                Tool.setStringToText(viewHolder.tvItemIMName, nickname);

            } else {
                Tool.setStringToText(viewHolder.tvItemIMName, targetInfo.getUserName());
            }

            Tool.setStringToText(viewHolder.tvItemIMContent, data.get(position).getLatestText());

        }

        return view;
    }

    static class ViewHolder {
        protected ImageView imageIMItemHeadPic;
        protected TextView tvItemIMName;
        protected TextView tvItemIMContent;
        protected LinearLayout layoutLiaoTian;

        ViewHolder(View rootView) {
            initView(rootView);
        }

        private void initView(View rootView) {
            imageIMItemHeadPic = (ImageView) rootView.findViewById(R.id.image_IM_item_headPic);
            tvItemIMName = (TextView) rootView.findViewById(R.id.tv_item_IM_name);
            tvItemIMContent = (TextView) rootView.findViewById(R.id.tv_item_IM_Content);
            layoutLiaoTian = (LinearLayout) rootView.findViewById(R.id.layout_LiaoTian);
        }
    }
}
